package net.langene.nlp.stanford;

//~--- non-JDK imports --------------------------------------------------------

import net.langene.common.ProgramUtils;
import net.langene.nlp.Context;
import net.langene.nlp.pg.PGDownloader;
import net.langene.nlp.pg.PGIndexer;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;

//~--- classes ----------------------------------------------------------------

/**
 * Hello world!
 *
 */
public class App {
    private static final String		_ACTION_PARSE_       = "parse";
    private static final String		_ACTION_PG_DOWNLOAD_ = "pg.download";
    private static final String		_ACTION_PG_INDEX_    = "pg.index";
    private static final String		_ACTION_PG_RESET_    = "pg.reset";
    private static final Logger		_logger              = LoggerFactory.getLogger(App.class);

    //~--- methods ------------------------------------------------------------

    /**
     *     Run the application.
     *
     *     @param args
     */
    public static void main(String[] args) {
        Options	options = new Options();
        Option	optconf = OptionBuilder.withArgName("conf").withLongOpt(
                             "config").hasArg().isRequired(true).withDescription(
                             "Application configuraiton file.").create("config");
        Option	optinput = OptionBuilder.withArgName("file").withLongOpt(
                              "input").hasArg().isRequired(false).withDescription(
                              "Text file containing input text.").create("input");
        Option	optdir = OptionBuilder.withArgName("dir").withLongOpt(
                            "directory").hasArg().isRequired(false).withDescription(
                            "Directory containing text files to generate POS tags for.").create(
                            "directory");
        Option	optact = OptionBuilder.withArgName("action").withLongOpt(
                            "action").hasArg().isRequired(false).withDescription(
                            "Action to perform.").create("action");

        options.addOption(optconf);
        options.addOption(optinput);
        options.addOption(optdir);
        options.addOption(optact);

        try {
            CommandLineParser	parser     = new PosixParser();
            CommandLine		cmd        = parser.parse(options, args);
            String		configfile = null;
            String		action     = null;

            if (cmd.hasOption("config")) {
                configfile = cmd.getOptionValue("config");
            } else {
                throw new Exception("Missing command line parameter -config.");
            }

            if (cmd.hasOption("action")) {
                action = cmd.getOptionValue("action");
            }

            Context.create(configfile);
            _logger.info("Configuration loaded from [" + configfile + "]...");

            if ((action == null) || action.isEmpty()
                    || (action.compareToIgnoreCase(_ACTION_PARSE_) == 0)) {
                parseAction(cmd);
            } else if (action.compareToIgnoreCase(_ACTION_PG_INDEX_) == 0) {
                pgIndexAction();
            } else if (action.compareToIgnoreCase(_ACTION_PG_DOWNLOAD_) == 0) {
                pgDownloadAction();
            } else if (action.compareToIgnoreCase(_ACTION_PG_RESET_) == 0) {
                pgResetAction();
            }
        } catch (Exception ex) {
            ProgramUtils.usage(App.class.getCanonicalName(), ex.getLocalizedMessage(), options);
        }
    }

    private static void pgResetAction() throws Exception {
        PGDownloader	downloader = new PGDownloader();

        downloader.setup();
        downloader.reset();
    }

    private static void pgIndexAction() throws Exception {
        PGIndexer	indexer = new PGIndexer();

        indexer.setup();
        indexer.process();
    }

    private static void pgDownloadAction() throws Exception {
        PGDownloader	downloader = new PGDownloader();

        downloader.setup();
        downloader.download();
    }

    private static void parseAction(CommandLine cmd) throws Exception {
        String	inputfile = null;
        String	directory = null;

        if (cmd.hasOption("input")) {
            inputfile = cmd.getOptionValue("input");

            File	fi = new File(inputfile);

            if (!fi.exists()) {
                throw new Exception("Invalid input file. File [" + inputfile + "] not found.");
            }
        }

        if (cmd.hasOption("directory")) {
            directory = cmd.getOptionValue("directory");

            File	fi = new File(directory);

            if (!fi.exists() ||!fi.isDirectory()) {
                throw new Exception("Invalid directory specified. Directory [" + directory
                                    + "] not found or is not a directory.");
            }
        }

        try {
            StanfordParser	parser = new StanfordParser();

            parser.setup();

            if ((inputfile != null) &&!inputfile.isEmpty()) {
                parser.parseFile(inputfile);
            } else if ((directory != null) &&!directory.isEmpty()) {
                parser.parseDirectory(directory);
            } else {
                parser.parse();
            }
        } catch (Exception nex) {
            nex.printStackTrace();
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
